package com.seari.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.seari.bean.*;
import com.seari.pojo.DataGridBean;
import com.seari.pojo.MessageBean;
import com.seari.service.DevStatusService;
import com.seari.service.UserService;
import com.seari.utils.ParseUtils;
import com.seari.utils.StringHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备状态监控
 *
 * @author hfm
 *
 */
@RestController
@RequestMapping("devStatus")
public class DevStatusController {

	@Autowired
	private DevStatusService devStatusService;

	@Autowired
	private UserService userService;



	/**
	 *  获取专业
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDomain")
	@ResponseBody
	public Map<String, Object> getDomain() {

		List<SelectObj> list = devStatusService.getDomain();

		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", json);
		return result;
	}

	/**
	 *  获取系统
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getSystem")
	@ResponseBody
	public Map<String, Object> getSystem(@RequestParam(value="parentId") Integer parentId) {

		List<SelectObj> list = devStatusService.getSystem(parentId);

		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", json);
		return result;
	}

	/**
	 *  获取设备
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDevice")
	@ResponseBody
	public Map<String, Object> getDevice(@RequestParam(value="parentId") Integer parentId) {

		List<SelectObj> list = devStatusService.getDevice(parentId);

		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", json);
		return result;
	}

	/**
	 * 报警管理>>>报警综合信息 站台下拉框
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getStation")
	@ResponseBody
	public Map<String, Object> getStation() {
		List<SelectObj> list = devStatusService.getStation();
		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", json);
		return result;
	}


	/**
	 * 报警数据分析>>>系统选择下拉框
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getSystems")
	@ResponseBody
	public Map<String, Object> getSystems() {
		List<String> list = devStatusService.getSystems();
		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", json);
		return result;
	}

	/**
	 * 报警数据分析>>>报警级别选择下拉框
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getAlarmLevel")
	@ResponseBody
	public Map<String, Object> getAlarmLevel() {
		List<SelectObj> list = devStatusService.getAlarmLevel();
		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", json);
		return result;
	}



	/**
	 * 报警管理>>>报警综合信息 状态下拉框
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getStatus")
	@ResponseBody
	public Map<String, Object> getStatus() {
		List<SelectObj> list = devStatusService.getStatus();
		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", json);
		return result;
	}

	/**
	 * 报警管理>>>报警综合信息 详情 维修方式下拉框
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getMaintenanceStatus")
	@ResponseBody
	public Map<String, Object> getMaintenanceStatus() {
		List<SelectObj> list = devStatusService.getMaintenanceStatus();
		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", json);
		return result;
	}

	/**
	 * 报警管理>>>报警综合信息 详情 工单状态下拉框
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getTaskStatus")
	@ResponseBody
	public Map<String, Object> getTaskStatus() {
		List<SelectObj> list = devStatusService.getTaskStatus();
		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", json);
		return result;
	}

	/**
	 * 报警管理>>>报警综合信息 有分页
	 *
	 * @return
	 */
	@RequestMapping("/alarm/getAlarmInfor")
	@ResponseBody
	public DataGridBean<AlarmInfor> getAlarmInfor(AlarmInfor alarmInfor) {
		DataGridBean<AlarmInfor> grid = new DataGridBean<AlarmInfor>();
		if (StringUtils.isNotBlank(alarmInfor.getCurrPage()) && StringUtils.isNotBlank(alarmInfor.getPageSize())) {
			alarmInfor.setStart(
					(Integer.parseInt(alarmInfor.getCurrPage()) - 1) * Integer.parseInt(alarmInfor.getPageSize()));
			alarmInfor.setLimit(Integer.parseInt(alarmInfor.getPageSize()));
		}
		List<AlarmInfor> list = devStatusService.getAlarmInfor(alarmInfor);
		if (list.size() > 0 && list != null) {
			grid.setRows(list);
			grid.setTotal(devStatusService.getAlarmInforListCount(alarmInfor));
		}
		return grid;
	}

	/**
	 * 报警管理>>>报警综合信息 没有分页
	 *
	 * @return
	 */
	@RequestMapping("/alarm/getAlarmInfor2")
	@ResponseBody
	public MessageBean getAlarmInfor2(String zhanTaiBianHao, String baoJingZhuangTai, String beginDate,
			String endDate) {
		MessageBean mb = new MessageBean();
		Map<String, Object> params = new HashMap<>();
		params.put("zhanTaiBianHao", zhanTaiBianHao);
		params.put("baoJingZhuangTai", baoJingZhuangTai);
		params.put("beginDate", beginDate);
		params.put("endDate", endDate);
		List<AlarmInfor> list = devStatusService.getAlarmInfor2(params);
		if (list != null && list.size() > 0) {
			mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
			mb.setMessage("报警综合信息");
			mb.setData(list);
		} else {
			mb.setCode(StringHelper.MESSAGE_TYPE_NULL);
			mb.setData(null);
		}
		return mb;
	}

	/**
	 * 报警管理>>>报警综合信息 详情列表
	 *
	 * @return
	 */
	@RequestMapping(value = "/getAlarmDetail")
	@ResponseBody
	public Map<String, Object> getAlarmDetail(String baoJingBianHao) {
		Map<String, Object> params = new HashMap<>();
		params.put("baoJingBianHao", baoJingBianHao);
		List<AlarmInfor> list = devStatusService.getAlarmDetail(params);
		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", json);
		return result;
	}

	/**
	 * 报警管理>>>报警综合信息 详情列表修改保存
	 *
	 * @return
	 */
	@RequestMapping(value = "/updateAlarmDetail")
	@ResponseBody
	public Map<String, Object> updateAlarmDetail(AlarmInfor alarmInfor, HttpServletRequest request) {
		Map<String, Object> params = new HashMap<>();
		params.put("baoJingBianHao", alarmInfor.getBaoJingBianHao());
		params.put("weiXiuRiQi", alarmInfor.getWeiXiuRiQi());
		params.put("weiXiuReng", alarmInfor.getWeiXiuReng());
		// gongDanZhuangTai 0：未处理 1：处理中 2：搁置 3：已处理
		params.put("gongDanZhuangTai", alarmInfor.getGongDanZhuangTai());
		params.put("weiXiuNeiRong", alarmInfor.getWeiXiuNeiRong());
		params.put("miaoShu", alarmInfor.getMiaoShu());
		params.put("productId", alarmInfor.getProductId());
		if (alarmInfor.getBaoJingZhuangTai().equals("0")) {// BaoJingZhuangTai=0 恢复 BaoJingZhuangTai=1 未处理
			params.put("lifeStatus", "7");// 7：报警恢复
		} else if (alarmInfor.getBaoJingZhuangTai().equals("1")) {
			params.put("lifeStatus", "6");
		}
		params.put("remark", alarmInfor.getMiaoShu());
		String cureateUserId = ParseUtils.getUidFromToken(request, userService);
		params.put("cureateUserId", cureateUserId);
		params.put("taskNo", alarmInfor.getRengWuHao());

		// baoJingZhuangTai 0:已恢复 1：未处理 2：处理中
		String baoJingZhuangTai = "1";
		if (alarmInfor.getGongDanZhuangTai().equals("0")) {// 1：未处理
			baoJingZhuangTai = "1";
		} else if (alarmInfor.getGongDanZhuangTai().equals("1") || alarmInfor.getGongDanZhuangTai().equals("2")) {// 2：处理中
			baoJingZhuangTai = "2";
		} else if (alarmInfor.getGongDanZhuangTai().equals("3")) {// 0:已恢复
			baoJingZhuangTai = "0";
		}
		params.put("baoJingZhuangTai", baoJingZhuangTai);
		// 业务表修改
		int count = devStatusService.updateAlarmDetail(params);
		// 生命周期表(报警表)
		int count2 = devStatusService.updateRtEvent(params);

		Map<String, Object> result = new HashMap<String, Object>();
		if (count >= 0 && count2 > 0) {
			result.put("result", StringHelper.MESSAGE_TYPE_SUCCESS);
		} else {
			result.put("result", StringHelper.MESSAGE_TYPE_ERROR);
		}
		return result;
	}

	/**
	 * 判断用户权限 能够查看哪条线路
	 *
	 * @return
	 */
	@RequestMapping(value = "/getRoles")
	@ResponseBody
	public Map<String, Object> getRoles(UserRoles userRoles) {
		List<UserRoles> list = devStatusService.getRoles(userRoles);
		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", json);
		return result;
	}


	/**
	 * 设备类型获取》》》下拉框
	 * @date 2019-12-25 10:12
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "/getdevTypeKey")
	@ResponseBody
	public Map<String, Object> getdevTypeKey() {
		List<String> list = devStatusService.getdevTypeKey();
		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<>();
		result.put("result", json);
		return result;
	}

	/**
	 * 资材编号获取》》》下拉框
	 * @date 2019-12-25 10:12
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "/getZJCode")
	@ResponseBody
	public Map<String, Object> getZJCode() {
		List<String> list = devStatusService.getZJCode();
		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<>();
		result.put("result", json);
		return result;
	}
	/**
	 * 获取设备维护计划表中的车站
	 *
	 * @return
	 */
	@RequestMapping(value = "/getStationPlan")
	@ResponseBody
	public Map<String, Object> getStationPlan(){

		List<SelectObj> list = devStatusService.getStationPlan();

		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();

		result.put("result",json);
		return result;
	}
	/**
	 * 获取线路信息
	 *
	 * @return
	 */
	@RequestMapping(value = "/getLine")
	@ResponseBody
	public DataGridBean<SelectObj> getLine(){
		DataGridBean<SelectObj> dataGridBean = new DataGridBean<SelectObj>();
		List<SelectObj> line = devStatusService.getLine();
		dataGridBean.setCode(200);
		dataGridBean.setTotal(line.size());
		dataGridBean.setRows(line);
		return dataGridBean;
	}

	/**
	 * 资材分类树状图
	 *
	 * @param req
	 * @return
	 */
	@RequestMapping("/getFMcTree")
	public DataGridBean<MaterialCaregory> getFMcTree(HttpServletRequest req)
	{
		DataGridBean<MaterialCaregory> grid = new DataGridBean<MaterialCaregory>();
		String fStatus = req.getParameter("fStatus");
		String fParent = req.getParameter("fParent");
		MaterialCaregory mc = new MaterialCaregory();
		if(StringUtils.isNotBlank(fStatus))
		{
			mc.setfStatus(fStatus);
		}
		if(StringUtils.isNotBlank(fParent))
		{
			mc.setfParent(fParent);
		}

		List<MaterialCaregory> materialCaregorys = devStatusService.getFMcTree(mc);
		grid.setRows(materialCaregorys);

		Map<String,Object> status = new HashMap();
		Map<String,List<MaterialCaregory>> basicData = new HashMap();
		basicData.put("basicDataList", materialCaregorys);
		status.put("code", 200);
		status.put("message", "操作成功！");
		grid.setStatus(status);
		grid.setBasicData(basicData);

		return  grid;
	}
	/**
	 * 获取车站信息
	 *
	 * @return
	 */
	@RequestMapping(value = "/getEtStation")
	@ResponseBody
	public DataGridBean<SelectObj> getEtStation(@Param("line_id") int line_id){
		DataGridBean<SelectObj> dataGridBean = new DataGridBean<SelectObj>();
		List<SelectObj> station = devStatusService.getEtStation(line_id);
		dataGridBean.setCode(200);
		dataGridBean.setTotal(station.size());
		dataGridBean.setRows(station);
		return dataGridBean;
	}
}
